package com.yc.sole.system.repository;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.text.CharSequenceUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yc.sole.common.enums.CommonState;
import com.yc.sole.framework.mybatis.page.Paging;
import com.yc.sole.framework.mybatis.util.PageUtil;
import com.yc.sole.system.dto.RoleDto;
import com.yc.sole.system.entity.Role;
import com.yc.sole.system.mapper.RoleMapper;
import com.yc.sole.system.request.RoleQueryReq;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collection;
import java.util.List;
import java.util.Objects;

/**
 * 服务接口实现
 *
 * @author yizuomin
 * @date 2023-03-26
 **/
@Slf4j
@Service
@RequiredArgsConstructor
public class RoleRepository extends ServiceImpl<RoleMapper, Role> {

    /**
     * 新增
     *
     * @param roleDto
     * @return RestResult
     */
    @Transactional(rollbackFor = Exception.class)
    public Long saveRole(RoleDto roleDto) {
        Role entity = new Role();
        BeanUtil.copyProperties(roleDto, entity);
        this.save(entity);
        return entity.getId();
    }

    /**
     * 通过Id修改
     *
     * @param roleDto
     * @return RestResult
     */
    @Transactional(rollbackFor = Exception.class)
    public void updateRoleById(RoleDto roleDto) {
        Role entity = new Role();
        BeanUtil.copyProperties(roleDto, entity);
        this.updateById(entity);
    }

    /**
     * 通过id删除
     *
     * @param ids
     * @return RestResult
     */
    @Transactional(rollbackFor = Exception.class)
    public void deleteRole(Collection<Long> ids) {
        this.removeBatchByIds(ids);
    }

    /**
     * 根据id获取
     *
     * @param id
     * @return RestResult
     */
    @Transactional(rollbackFor = Exception.class)
    public RoleDto selectRoleById(Long id) {
        return BeanUtil.copyProperties(this.getById(id), RoleDto.class);
    }

    /**
     * 根据id获取
     *
     * @param ids
     * @return RestResult
     */
    @Transactional(rollbackFor = Exception.class)
    public List<RoleDto> selectRoleByIds(Collection<Long> ids) {
        return BeanUtil.copyToList(this.listByIds(ids), RoleDto.class);
    }

    /**
     * 查询所有角色
     *
     * @return
     */
    public List<RoleDto> selectAll() {
        LambdaQueryWrapper<Role> wrapper = Wrappers.<Role>lambdaQuery().eq(Role::getState, CommonState.ENABLE);
        return BeanUtil.copyToList(this.list(wrapper), RoleDto.class);
    }

    /**
     * 分页查询
     *
     * @param page
     * @return RestResult
     */
    public Paging<RoleDto> selectRolePage(Paging page, RoleQueryReq roleQueryReq) {
        LambdaQueryWrapper<Role> queryWrapper = Wrappers.<Role>lambdaQuery()
                .like(CharSequenceUtil.isNotBlank(roleQueryReq.getRoleCode()), Role::getRoleCode, roleQueryReq.getRoleCode())
                .like(CharSequenceUtil.isNotBlank(roleQueryReq.getRoleName()), Role::getRoleName, roleQueryReq.getRoleName())
                .eq(Objects.nonNull(roleQueryReq.getState()), Role::getState, roleQueryReq.getState());
        return PageUtil.pageCopy(this.page(PageUtil.toMybatisPage(page), queryWrapper), RoleDto.class);
    }
}
